//
// Created by martin on 1/28/22.
//

#ifndef MYMUDUO_TYPES_H
#define MYMUDUO_TYPES_H

#include <stdint.h>
#include <string.h>
#include <string>

#ifndef NDEBUG // Not Debug
#include <assert.h>
#endif

/**
 * The most common stuffs.
 */
namespace muduo
{
using std::string;

inline void memZero(void* p, size_t n)
{
    memset(p, 0, n);
}

/*
 * Taken from google-protobuf stubs/common.h
 * Protocol Buffers - Google's data interchange format
 * Copyright 2008 Google Inc.  All rights reserved.
 * http://code.google.com/p/protobuf/
 */

/*
 * Use implicit_cast as a safe version of static_cast or const_cast
 * for upcasting in the type hierarchy (i.e. casting a pointer to Foo
 * to a pointer to SuperclassOfFoo or casting a pointer to Foo to
 * a const pointer to Foo).
 *
 * The From type can be inferred, so the preferred sytax for using
 * implicit_cast is the same as for static_cast etc.:
 *
 *   implicit_cast<ToType>(expr)
 *
 * implicit_cast would have been part of the C++ standard library,
 * but the proposal was submitted too late. It will probably make
 * its way into language in the future.
 */
template<typename To, typename From>
inline To implicit_cast(From const& f)
{
    return f;
}

/*  */
template<typename To, typename From>
inline To down_cast(From* f)
{
    /* Ensures that To is a sub-type of From*. This test is here only
     for compile-time checking, and has no overhead in an
     optimized build at run-time, as it will be optimized away
     completely. */
    if (false)
    {
        implicit_cast<From*, To>(0);
    }
#if !defined(NDEBUG) && !defined(GOOGLE_PROTOBUF_NO_RTTI)
    assert(f == NULL || dynamic_cast<To>(f) != NULL); // RTTI: debug mode only!
#endif
    return static_cast<To>(f);
}

} // namespace muduo

#endif //MYMUDUO_TYPES_H
